// Package promise 监控window桌面
package promise

import (
	"bufio"
	"encoding/json"
	"fmt"
	"log"
	"os"
	"runtime"
	"strings"
	"time"

	"gitee.com/wang_cyi/TryEverything/public"
	"github.com/jakecoffman/cron"
)

//TaskMsg TaskMsg
type TaskMsg struct {
	Type       string `json:"type"`
	Content    string `json:"content"`
	IsRepeated string `json:"isrepeated"`
	BeginTime  string `json:"begintime"`
	EndTime    string `json:"endtime"`
	IsDone     string `json:"isdone"`
}

// DailyTask DailyTask
type DailyTask struct {
	Code    string
	Param   string
	Queue   chan string
	RtmpMap map[string]string
}

// Run Run
func (dt DailyTask) Run() {
	//晚上22:00自动发表每日总结 一天一次
	if dt.Code == TASKTYPEDAY {
		msg := ProductTaskFromFile(dt.Param)
		log.Println("系统之美-下班-每日复盘", msg)
		if len(msg) == 0 {
			return
		}

		//struct to json
		taskMsg := TaskMsg{
			Type:       "1",
			Content:    msg,
			IsRepeated: "0",
			BeginTime:  " ",
			EndTime:    " ",
			IsDone:     "",
		}

		jsonData, _ := json.Marshal(taskMsg)

		fmt.Println(string(jsonData))
		dt.Queue <- string(jsonData) //插入消息
	} else if dt.Code == "2" {

		if len(dt.Param) == 0 {
			return
		}

		msg := "晨间日记 "
		msg += "\r\n"
		msg += dt.Param

		//struct to json
		taskMsg := TaskMsg{
			Type:       "2",
			Content:    msg,
			IsRepeated: "0",
			BeginTime:  " ",
			EndTime:    " ",
			IsDone:     "",
		}

		jsonData, _ := json.Marshal(taskMsg)

		fmt.Println(string(jsonData))
		log.Println(string(jsonData))
		dt.Queue <- string(jsonData) //插入消息

	} else if dt.Code == "4" {
		//task 直播
		input := dt.Param
		output := dt.RtmpMap["douyu"]

		var ffmeg public.FfmPegTool
		if ffmeg.Mp4ToRtmp(input, output) == false {
			//邮件告警
			public.SendMailBy163("直播失败")
		}
	}
}

//TaskReadDeal other
func TaskReadDeal(msg string) {
	if len(msg) == 0 {
		log.Println("msg is null")
		return
	}
	task := TaskMsg{}
	json.Unmarshal([]byte(msg), &task)
	log.Println(task.Type)
	switch task.Type {
	case "1":
	case "2":
		//每日一句话总结
		//渠道 邮箱，微博
		log.Println("ask_deal", task.Content)
		public.SendMailBy163(task.Content)
		if true {

			task.Content += " https://wangcy6.github.io"
			if len(task.Content) > 140 {
				log.Println(" more than 140 ", len(task.Content))
				return
			}
			public.WeiboDailyThinking(task.Content)
		}

	default:
		log.Println("err err err err err err task_deal", msg)
	}
}

//ProductTaskFromFile 读取日报文件
func ProductTaskFromFile(path string) string {
	file, err := os.Open(path)
	if err != nil {
		fmt.Println(err)
	}
	defer file.Close()
	now := time.Now()
	time1 := now.Format("2006-01-02 15:04:05")
	time2 := now.Format("2006年1月2日")
	var buffer []string
	scanner := bufio.NewScanner(file)
	var lines string

	for scanner.Scan() {
		lines = scanner.Text()
		if strings.Contains(lines, "---") == true {
			//fmt.Println("-------- ")
			break
		}
		//fmt.Println(lines)
		buffer = append(buffer, lines)

	}
	if len(buffer) == 0 {
		return ""
	}

	if strings.Contains(buffer[0], time1) || strings.Contains(buffer[0], time2) {
		//fmt.Println("today", buffer[0], time1, time2)
		temp := buffer[0]
		temp += "[按时记录]"
		buffer[0] = temp

	} else {
		//fmt.Println("delay", buffer[0], time1, time2)
		temp := buffer[0]
		temp += "[延迟记录]"
		buffer[0] = temp
	}

	var result string

	for _, data := range buffer {
		result += data
		result += "\r\n"
	}
	fmt.Println(result)
	return result

}

//RtmpShareTask 直播类任务
func RtmpShareTask(rtmpMap map[string]string) {
	//定时调度
	if runtime.GOOS != "linux" {
		return
	}
	cron := cron.New()

	taskName1 := "早晨"
	path1 := "/root/code/mp4/keep/01.mp4"
	cron.AddJob("0 30 5 * * *", DailyTask{"4", path1, nil, rtmpMap}, taskName1)

	taskName2 := "上午"
	path2 := "/root/code/mp4/go/12.mp4"
	cron.AddJob("0 0 9 * * *", DailyTask{"4", path2, nil, rtmpMap}, taskName2)

	taskName3 := "下午"
	path3 := "/root/bbc/1.mp4"
	cron.AddJob("0 0 6 * * *", DailyTask{"4", path3, nil, rtmpMap}, taskName3)

	taskName := "晚上"
	path := "/root/code/mp4/deep_sleep.mp4"
	cron.AddJob("0 10 21 * * *", DailyTask{"4", path, nil, rtmpMap}, taskName)

}
